package kr.or.kead.module;



import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Calendar;

import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JLabel;

@SuppressWarnings("serial")
public class RegDate extends JPanel {
	private JComboBox<Integer> comboYear;
	private JComboBox<Integer> comboMonth;
	private JComboBox<Integer> comboDay;
	
	public RegDate() {		
		init();		
	}
	
	private void init() {
		final Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);
		int nextYear = year+5;
		
		comboYear = new JComboBox<>();
		for(int i=year-5;i< nextYear; i++) {
			comboYear.addItem(i);
		}
		comboYear.setSelectedItem(year);
		comboYear.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				comboDay.removeAllItems();			
				now.set(Calendar.YEAR, comboYear.getSelectedIndex());
				now.set(Calendar.MONTH, comboMonth.getSelectedIndex());
				int maxDay = now.getActualMaximum(Calendar.DATE);				
				for(int i=1;i<=maxDay;i++) {
					comboDay.addItem(i);
				}				
				comboDay.updateUI();
			}
		});
		add(comboYear);
		
		JLabel lblNewLabel = new JLabel("년");
		add(lblNewLabel);
		
		comboMonth = new JComboBox<>();
		for(int i=1; i< 13 ; i++) {
			comboMonth.addItem(i);
		}
		comboMonth.setSelectedIndex(now.get(Calendar.MONTH));
		add(comboMonth);
		comboMonth.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				comboDay.removeAllItems();			
				now.set(Calendar.MONTH, comboMonth.getSelectedIndex());
				int maxDay = now.getActualMaximum(Calendar.DATE);				
				for(int i=1;i<=maxDay;i++) {
					comboDay.addItem(i);
				}
				System.out.println(now.get(Calendar.YEAR));
				comboDay.updateUI();
			}
		});
		
		JLabel lblNewLabel_1 = new JLabel("월");
		add(lblNewLabel_1);
		
		comboDay = new JComboBox<>();
		int maxDay = now.getActualMaximum(Calendar.DATE);
		System.out.println(maxDay);
		for(int i=1;i<maxDay;i++) {
			comboDay.addItem(i);
		}
		
//		comboDay.addItem(now.get(Calendar.DATE));
		add(comboDay);
		comboDay.setSelectedItem(11);
		
		JLabel lblNewLabel_2 = new JLabel("일");
		add(lblNewLabel_2);
	}
	
	public int getYear() {return (int)comboYear.getSelectedItem();}
	public int getMonth() {return (int)comboMonth.getSelectedItem();}
	public int getDay() {return (int)comboDay.getSelectedItem();}
	
	public void setTxtDate(int year, int month, int day) {
		comboYear.setSelectedItem(year);
		comboMonth.setSelectedItem(month);
		comboDay.setSelectedItem(day);
	}
	
	public String toString() {
		return String.format("%s-%s-%s", getYear(), getMonth(), getDay());
	}

}
